﻿/*
* menu
*/
$(function () {
    // menu
    (function () {
        var initPId = $.cookie("parent") ? $.cookie("parent") : 'nav-home';

        var resetTimer = 0;

        (function () {
            var parent = "#" + initPId;
            $("#nav").find(parent).addClass("current");
        })();


        (function () {
            $("#sub-navs a,#nav-home").click(function (e) {
                if ($(this).parent().attr("pnav") !== initPId) {
                    $.cookie('parent', '', { expires: -1, path: '/' });
                    $.cookie('parent', $(this).parent().attr("pnav"), { expires: 7, path: '/' })
                }
            });
        })();

        function reset() {
            $("#nav>li[id!='" + initPId + "']").removeClass('current');
            $("#sub-navs>.subnav").css("display", "none");
            $("#sub-navs>.subnav[pnav='" + initPId + "']").css("display", "block");
        }

        (function () {
            $("#nav>li").each(function () {
                if ($(this).attr("className").indexOf('current') !== -1) {
                    initPId = $(this).attr('id');
                    return false;
                }
            });
        })();

        // init sub nav.
        $("#sub-navs>.subnav").each(function () {
            var pid = $.trim($(this).attr("pnav"));
            if (pid === '') {
                return;
            }

            try {
                var p = $("#" + pid);
                if (p.length !== 1) {
                    return;
                }

                var pLeft = p.offset().left;
                var pWidth = p.width();
                var wWidth = $(window).width();
                var width = $(this).width();
                var left = pLeft + pWidth / 2 - width / 2;
                left = (left < 10) ? 10 : parseInt(left.toString(), 10);
                left = (left + width > wWidth) ? wWidth - width : left;

                $(this).css({ "marginLeft": left + 'px' });

                if (p.attr('className').indexOf('current') !== -1) {
                    $(this).css("display", "block");
                    initPId = pid;
                }
            } catch (e) { }
        });
        //        .hover(function () {
        //            if (resetTimer !== 0) {
        //                clearTimeout(resetTimer);
        //            }
        //        }, reset);

        //        $("#nav>li").hover(function () {
        //            var cid = $(this).attr('id');
        //            if (cid !== initPId) {
        //                $(this).addClass('current');
        //            }

        //            if (resetTimer !== 0) {
        //                clearTimeout(resetTimer);
        //            }

        //            $("#nav>li[id!='" + cid + "'][id!='" + initPId + "']").removeClass('current');
        //            $("#sub-navs>.subnav").css("display", "none");
        //            $("#sub-navs>.subnav[pnav='" + cid + "']").css("display", "block");
        //        }, function () {
        //            var cid = $(this).attr('id');
        //            if (cid !== initPId) {
        //                resetTimer = setTimeout(reset, 200);
        //            }
        //        });


        $("#nav>li").click(function () {
            var cid = $(this).attr('id');
            if (cid !== initPId) {
                $(this).addClass('current');
            }
            if (resetTimer !== 0) {
                clearTimeout(resetTimer);
            }

            $("#nav>li[id!='" + cid + "'][id!='" + initPId + "']").removeClass('current');
            $("#sub-navs>.subnav").css("display", "none");
            $("#sub-navs>.subnav[pnav='" + cid + "']").css("display", "block");
            ///


            var p = $(this);
            if (p.length !== 1) {
                return;
            }

            var pLeft = p.offset().left;
            var pWidth = p.width();
            var wWidth = $(window).width();
            var width = $(this).width();
            var left = pLeft + pWidth / 2 - width / 2;
            left = (left < 10) ? 10 : parseInt(left.toString(), 10);
            left = (left + width > wWidth) ? wWidth - width : left;

            $("sub-navs>.subnav").filter("[pid=" + $(this).attr('id') + "]").css({ "marginLeft": left + 'px' });

            if (p.attr('className').indexOf('current') !== -1) {
                $(this).css("display", "block");
                //initPId = $(this).attr("id");
            }

        });
    })(); // end of menu

    // enter key
    (function () {
        var uiElePattern = 'input[type="text"],input[type="radio"],select';
        $(uiElePattern).keydown(function (e) {
            if (e.keyCode === 13) {
                var n = getNextUIElement($(this));
                if (n !== null) {
                    n.focus();
                    if (n.select)
                        n.select();
                }

                e.preventDefault();
            }
        }).bind("dblclick", function (e) {
            var n = $(this)[0];
            if (n.select) {
                n.select();
                e.preventDefault();
            }
        });

        function getNextUIElement(ce) {
            var d = ce.find(uiElePattern);
            if (d.length !== 0 && !$(d[0]).attr('disabled')) {
                return d[0];
            }

            var next = ce.nextAll(uiElePattern);
            if (next.length === 0) {
                next = ce.next();
                if (next.length !== 0) {
                    return getNextUIElement(next);
                }
                else {
                    var p = getParentNextAvail(ce);

                    if (p === null) { return null; }
                    else { return getNextUIElement(p); }
                }
            } else {
                var r = $(next[0]);
                if (!r.attr("disabled")) {
                    return getNextUIElement($(next[0]));
                }
                return r[0];
            }
        }

        function getParentNextAvail(ce) {
            if (ce.is('body') || ce.is('html')) { return null; }

            var p = ce.parent();
            var pn = p.next();
            if (pn.length === 0) {
                return getParentNextAvail(p);
            } else {
                return pn;
            }
        }
    })();

    // loading
    (function () {
        var success = false;
        $("#loading").ajaxStart(function () {
            success = true;
            $(this).css("color", "").html('操作进行中...').show();
        }).ajaxStop(function () {
            if (success) {
                $(this).hide();
            }
        }).ajaxError(function () {
            success = false;
            $(this).css("color", "#db1010").html('系统发生错误，请检查您的输入或者稍后再试!');
        });

        if (!$.browser.msie) {
            $(window).bind("beforeunload", function () { $("#loading").css("color", "").html('页面加载中...').show(); });
        }
    })();
});